package com.kaiyidiannao.activity.user_detail;

import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
import com.kaiyidiannao.common.BaseActionBarActivity;
import com.kaiyidiannao.common.util.AppUtils;
import com.kaiyidiannao.data.DataCenter;
import com.kaiyidiannao.data.Validate;
import com.kaiyidiannao.https.HttpUtils;
import com.kaiyidiannao.main.R;
import com.kaiyidiannao.model.bean.UserBean;
import com.kaiyidiannao.model.req.model.GetEditUserDatail;
import com.kaiyidiannao.view.ActionBarView;
import com.taplinker.core.util.LogUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

/**
 * Created by wzg on 14-4-16.
 */
public class EditUserDetail extends BaseActionBarActivity implements Observer {
    private EditText user_detail_edit_username;
    private EditText user_detail_edit_truename;
    private EditText user_detail_edit_tel;
    private EditText user_detail_edit_address;
    private EditText user_detail_edit_qq;
    private ImageButton determine;

    private List<UserBean> userBeans = DataCenter.userLogin;
    private ArrayList<UserBean> userBeanlist = DataCenter.userBeans;
    private String username;
    private String truename;
    private String address;
    private String qq;
    private String mobile;

    UserBean userBean;
    String userid;

    private SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit_user_detail);
        AppUtils.setupUI(findViewById(R.id.edit_user), this);
        // 添加观察者
        DataCenter.getInstance().addObserver(this);
        init();
    }

    public void init() {
        findActionBar();
        user_detail_edit_address = (EditText) findViewById(R.id.user_detail_edit_address);
        user_detail_edit_truename = (EditText) findViewById(R.id.user_detail_edit_truename);
        user_detail_edit_tel = (EditText) findViewById(R.id.user_detail_edit_tel);
        user_detail_edit_username = (EditText) findViewById(R.id.user_detail_edit_username);
        user_detail_edit_qq = (EditText) findViewById(R.id.user_detail_edit_qq);
        determine = (ImageButton) findViewById(R.id.determine);

        Intent intent = getIntent();
        userBean = (UserBean) intent.getSerializableExtra("userbean");
        user_detail_edit_address.setText(userBeanlist.get(0).getAddress());
        user_detail_edit_truename.setText(userBeanlist.get(0).getTruename());
//        user_detail_edit_tel.setText(userBean.getMobile());
        user_detail_edit_username.setText(userBeanlist.get(0).getUsername());
        user_detail_edit_qq.setText(userBeanlist.get(0).getQq());
        user_detail_edit_tel.setText(userBeanlist.get(0).getMobile());
        //修改确认事件
        determine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                truename = user_detail_edit_truename.getText().toString();
                username = user_detail_edit_username.getText().toString();
                address = user_detail_edit_address.getText().toString();
                qq = user_detail_edit_qq.getText().toString();
                mobile = user_detail_edit_tel.getText().toString();
                Log.d("truename", truename);
                if (!"".equals(truename) || truename == null) {
                    if (Validate.truenameValidate(truename)) {
                        if (!"".equals(username) || username == null) {
                            if (username.length() >= 2 && username.length() <= 8) {
                                if (Validate.search(username)) {
                                    if (!"".equals(mobile) || mobile == null) {
                                        if (mobile.length() == 11) {
                                            if (mobile.matches(Validate.phoneValidate)) {
                                                if (!"".equals(address) || address == null) {
                                                    if (address.length() <= 30 && address.length() >= 6) {
                                                        if (address.matches(Validate.addressValidate)) {
                                                            if (!"".equals(qq) || qq == null) {
                                                                if (qq.length() >= 4 && qq.length() <= 10) {
                                                                    if (qq.matches(Validate.qqValidate)) {
                                                                        showProgressDialog("正在提交");

                                                                        if (userBeans == null || userBeans.size() == 0) {
                                                                            closeProgressDialog();
                                                                            showToast(R.string.no_network);
                                                                            return;
                                                                        } else {
                                                                            userid = userBeans.get(0).getUserid();
                                                                            new GetEditUserDatail(userid, username, truename, address, qq, "1").execute();
                                                                        }
                                                                    } else {
                                                                        Log.d("qq--", "-=-=-=-=-=-=-");
                                                                        AlertDialog builder = new AlertDialog.Builder(EditUserDetail.this).setTitle("错误信息")
                                                                                .setMessage("QQ为4-10位，只能输入数字，不能输入空格").setPositiveButton("确定", null).show();
                                                                    }
                                                                } else {
                                                                    Log.d("qq--", "=-=-=-=-=-=");
                                                                    AlertDialog builder = new AlertDialog.Builder(EditUserDetail.this).setTitle("错误信息")
                                                                            .setMessage("QQ为4-10位，只能输入数字，不能输入空格").setPositiveButton("确定", null).show();
                                                                }

                                                            } else {
                                                                AlertDialog builder = new AlertDialog.Builder(EditUserDetail.this).setTitle("错误信息")
                                                                        .setMessage("QQ不能为空").setPositiveButton("确定", null).show();
                                                            }

                                                        } else {
                                                            AlertDialog builder = new AlertDialog.Builder(EditUserDetail.this).setTitle("错误信息")
                                                                    .setMessage("地址为6-30位，不能输入空格").setPositiveButton("确定", null).show();
                                                        }

                                                    } else {
                                                        AlertDialog builder = new AlertDialog.Builder(EditUserDetail.this).setTitle("错误信息")
                                                                .setMessage("地址为6-30位，不能输入空格").setPositiveButton("确定", null).show();
                                                    }

                                                } else {
                                                    AlertDialog builder = new AlertDialog.Builder(EditUserDetail.this).setTitle("错误信息")
                                                            .setMessage("地址不能为空").setPositiveButton("确定", null).show();
                                                }

                                            } else {
                                                AlertDialog builder = new AlertDialog.Builder(EditUserDetail.this).setTitle("错误信息")
                                                        .setMessage("手机号为11位，只能输入数字，不能输入空格").setPositiveButton("确定", null).show();
                                            }

                                        } else {
                                            AlertDialog builder = new AlertDialog.Builder(EditUserDetail.this).setTitle("错误信息")
                                                    .setMessage("手机号为11位，只能输入数字，不能输入空格").setPositiveButton("确定", null).show();
                                        }
                                    } else {
                                        AlertDialog builder = new AlertDialog.Builder(EditUserDetail.this).setTitle("错误信息")
                                                .setMessage("手机号不能为空").setPositiveButton("确定", null).show();
                                    }

                                } else {
                                    AlertDialog builder = new AlertDialog.Builder(EditUserDetail.this).setTitle("错误信息")
                                            .setMessage("昵称为2-8位，不能输入空格").setPositiveButton("确定", null).show();
                                }
                            } else {
                                AlertDialog builder = new AlertDialog.Builder(EditUserDetail.this).setTitle("错误信息")
                                        .setMessage("昵称为2-8位，不能输入空格").setPositiveButton("确定", null).show();
                            }

                        } else {
                            AlertDialog builder = new AlertDialog.Builder(EditUserDetail.this).setTitle("错误信息")
                                    .setMessage("昵称不能为空").setPositiveButton("确定", null).show();
                        }

                    } else {
                        AlertDialog builder = new AlertDialog.Builder(EditUserDetail.this).setTitle("错误信息")
                                .setMessage("姓名为2-5位，不能输入空格").setPositiveButton("确定", null).show();
                    }

                } else {
                    AlertDialog builder = new AlertDialog.Builder(EditUserDetail.this).setTitle("错误信息")
                            .setMessage("姓名不能为空").setPositiveButton("确定", null).show();
                }

            }
        });

    }

    private void findActionBar() {
        actionBarView = (ActionBarView) findViewById(R.id.actionbar);
        actionBarView.setTitle("编辑个人资料");
        actionBarView.checkBox.setVisibility(View.GONE);
    }

    @Override
    public void update(Observable observable, Object data) {
        DataCenter.flag flag = (DataCenter.flag) data;
        if (!HttpUtils.isConnected()) {
            closeProgressDialog();
            return;
        }
        if (flag == DataCenter.flag.GET_EDIT_USER_DETAIL_SUCCESS
                || flag == DataCenter.flag.GET_EDIT_USER_DETAIL_FAILED) { //类表数据

            String code = DataCenter.code;
            if (flag == DataCenter.flag.GET_EDIT_USER_DETAIL_SUCCESS) {
                if (code.equals(DataCenter.successFlag)) {
                    closeProgressDialog();
//                    saveUserDetail();
                    Toast.makeText(EditUserDetail.this, DataCenter.msg, Toast.LENGTH_SHORT).show();
                    finish();
//                    Intent intent = new Intent(EditUserDetail.this, UserDetail.class);
//                    startActivity(intent);

                } else {
                    closeProgressDialog();
                    Toast.makeText(EditUserDetail.this, DataCenter.msg, Toast.LENGTH_SHORT).show();
                    return;
                }
            }

            if (flag == DataCenter.flag.GET_EDIT_USER_DETAIL_FAILED) {
                closeProgressDialog();
                // Toast.makeText(EditUserDetail.this, "failed", Toast.LENGTH_SHORT).show();
            }
        }
    }

//    public void saveUserDetail() {
//        sharedPreferences = getSharedPreferences("user_detail", Context.MODE_PRIVATE);
//        SharedPreferences.Editor editor = sharedPreferences.edit();
//        editor.putString("userid", userBeans.get(0).getUserid());
//        editor.putString("mobile", mobile);
//        editor.putString("address", address);
//        editor.putString("username", username);
//        editor.putString("truename", truename);
//        editor.putString("password", DataCenter.password);
//        editor.putString("qq", qq);
//        editor.commit();
//    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtil.d(DataCenter.LOG_INFO, EditUserDetail.class.getSimpleName() + ".onDestroy");
        //删除观察者
        DataCenter.getInstance().deleteObserver(this);
    }

}
